package org.example.erp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.erp.domain.SysMenu;
import org.example.erp.myInterface.NodeMapper;
import org.example.erp.service.SysMenuService;
import org.example.erp.mapper.SysMenuMapper;
import org.example.erp.utils.TreeBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2024-11-04 09:35:45
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{

    @Autowired
    SysMenuMapper sysMenuMapper;

    public List<SysMenu> findMenus(String username, String menuType) {
        List<SysMenu> menus;
        if (menuType == null) {
            menus = sysMenuMapper.findMenus(username, null);
            return menus;
        }
        menus = sysMenuMapper.findMenus(username, menuType);
        TreeBuild<SysMenu> treeBuild = new TreeBuild<>();
        // 创建NodeMapper实例
        NodeMapper<SysMenu> sysMenuNodeMapper = TreeBuild.createNodeMapper(
                SysMenu::getMenuId,
                SysMenu::getParentId,
                SysMenu::getChildren,
                SysMenu::setChildren
        );
        return treeBuild.buildTree(menus, sysMenuNodeMapper);
    }

    public List<SysMenu> findMenuCascade(String menuName, Integer status){
        List<SysMenu> menuList = baseMapper.findMenuCascade(menuName,status);
        TreeBuild<SysMenu> treeBuild = new TreeBuild<>();
        //创建NodeMapper实例
        NodeMapper<SysMenu> sysMenuNodeMapper = TreeBuild.createNodeMapper(
                SysMenu::getMenuId,
                SysMenu::getParentId,
                SysMenu::getChildren,
                SysMenu::setChildren
        );
        return treeBuild.buildTree(menuList,sysMenuNodeMapper);
    }

    public List<SysMenu> findBySys_RoleId(Integer roleId){
        return baseMapper.findBySys_RoleId(roleId);
    }

}




